/**
 * Copyright (C), 2015-2019, 静修教育咨询有限公司
 * FileName: ChitchatServiceImpl
 * Author:  SAGIRI
 * Date:     2019/7/24 0024 11:35
 * Description: 聊天与回复业务实现类
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.ruimei.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.mongodb.client.result.UpdateResult;
import com.ruimei.entitys.Chitchat;
import com.ruimei.entitys.ParentAndStudent;
import com.ruimei.entitys.Student;
import com.ruimei.entitys.Teacher;
import com.ruimei.mapper.ChitchatMapper;
import com.ruimei.mapper.UserMapper;
import com.ruimei.service.IChitchatService;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 〈概要〉<br> 
 * 〈聊天与回复业务实现类〉
 *
 * @author SAGIRI
 * @create 2019/7/24 0024
 * @since 1.0.0
 */
@Service
public class ChitchatServiceImpl implements IChitchatService {
    @Resource
    private ChitchatMapper chitchatMapper;
    @Resource
    private MongoTemplate mongoTemplate;


    @Override
    public JSONObject sendMessage(List<ParentAndStudent> studentList, String teacherName, String content, String teacherId) {
        JSONObject object = new JSONObject();
        JSONObject data = new JSONObject();
        //定义一个即将存入mongodb的容器
        List<Chitchat> chitchats = new ArrayList<>();
        //封装属性
        for(ParentAndStudent s :studentList){
            Chitchat chitchat = new Chitchat();
            chitchat.set_id(UUID.randomUUID().toString().replace("-",""));
            chitchat.setAddTime(new Date());
            chitchat.setContent(content);
            chitchat.setStudentName(s.getStudentName());
            chitchat.setStudentId(s.getStudentId());
            chitchat.setTeacherId(teacherId);
            chitchat.setTeacherName(teacherName);
            chitchat.setTeacherStatus(true);
            chitchat.setParentStatus(false);
            chitchat.setReplyTime(new Date());
            chitchat.setParentName("");
            chitchat.setType(1);
            chitchats.add(chitchat);
        }
        mongoTemplate.insert(chitchats,"Chitchat");
        //尝试手动gc
        System.gc();
        data.put("status",true);
        data.put("msg","发送成功");
        object.put("status",true);
        object.put("data",data);
        return object;
    }

    @Override
    public JSONObject sendMessageToTeacher(List<String> teacherList, String parentName, String content,String studentName,
                                           String studentId,String parentId) {
        JSONObject object = new JSONObject();
        JSONObject data = new JSONObject();
        //定义一个即将存入mongodb的容器
        List<Chitchat> chitchats = new ArrayList<>();
        //首先拉取所有家长的信息。
        List<String> list = chitchatMapper.selectTeacherName(teacherList);
        //查关系
        String relation = chitchatMapper.selectRelation(parentId,studentId);
        //封装属性
        //两个集合长度一致 使用一个循环操作即可
        for(int i = 0;i<teacherList.size();i++){
            Chitchat chitchat = new Chitchat();
            chitchat.set_id(UUID.randomUUID().toString().replace("-",""));
            chitchat.setAddTime(new Date());
            chitchat.setContent(content);
            chitchat.setTeacherId(teacherList.get(i));
            chitchat.setTeacherName(list.get(i));
            chitchat.setTeacherStatus(false);
            chitchat.setParentStatus(true);
            chitchat.setType(1);
            chitchat.setStudentName(studentName);
            chitchat.setStudentId(studentId);
            chitchat.setReplyTime(new Date());
            chitchat.setParentName(parentName);
            chitchat.setRelation(relation);
            chitchats.add(chitchat);
        }
        mongoTemplate.insert(chitchats,"Chitchat");
        //尝试手动gc
        System.gc();
        data.put("status",true);
        data.put("msg","发送成功");
        object.put("status",true);
        object.put("data",data);
        return object;
    }

    @Override
    public JSONObject sendReply(String replyContent,String teacherName,String parentName,String teacherId,String documentId,String type
    ,String studentId,String parentId) {
        JSONObject object = new JSONObject();
        JSONObject data = new JSONObject();
        String relation = null;
        //查询关系
        if(StringUtils.isNotBlank(parentId)){
            relation =  chitchatMapper.selectRelation(parentId,studentId);
        }
        //首先添加新的信息。
        Chitchat chitchat = new Chitchat();
        Update update = null;
        chitchat.set_id(UUID.randomUUID().toString().replace("-",""));
        chitchat.setType(2);
        chitchat.setTeacherName(teacherName);
        chitchat.setParentName(parentName);
        chitchat.setPid(documentId);
        chitchat.setContent(replyContent);
        chitchat.setTeacherId(teacherId);
        chitchat.setAddTime(new Date());
        chitchat.setStudentId(studentId);
        if(type.equals("parent")){
            chitchat.setRelation(relation);
        }else{
            chitchat.setRelation("");
        }
        mongoTemplate.insert(chitchat,"Chitchat");
        Query query = new Query();
        //然后更改该消息的状态
        query.addCriteria(Criteria.where("_id").is(documentId));
        if(type.equals("teacher")){
            update = Update.update("parentStatus", false).set("replyTime",new Date());
        }
        if(type.equals("parent")){
            update = Update.update("teacherStatus", false).set("replyTime",new Date());
        }

        UpdateResult upsert = mongoTemplate.upsert(query, update,"Chitchat");
        if(upsert.getMatchedCount()>0){
            data.put("status",true);
            data.put("msg","发送成功");
            object.put("status",true);
            object.put("data",data);
            return data;
        }else{
            data.put("status",false);
            data.put("msg","消息未能成功发送，请稍后再试");
            object.put("status",true);
            object.put("data",data);
            return data;
        }

    }

    @Override
    public Map<String, Object> getTeacherByClassId(String classId) {
        List<Teacher> teacherList = chitchatMapper.getTeacherByClassId(classId);
        Map<String,Object> map = new HashMap<>();
        map.put("status", true);
        map.put("message", "查询成功");
        map.put("data", teacherList);
        return map;
    }
}
